using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class SeasonEventButton : MonoBehaviour
{
	public List<UISprite> stars = new List<UISprite>();

	public UILabel titleLabel;

	public UILabel subtitleLabel;

	public UISprite headerSprite;

	public Color starOffColor;

	public SeasonTournamentLayer tournamentLayer;

	protected SeasonDefinitions.SeasonEvent seasonEvent;

	protected SeasonDefinitions.SeasonInterval seasonInterval;

	protected CareerSeasonLayer seasonLayer;

	protected bool isEventFinished;

	public void Init(SeasonDefinitions.SeasonEvent seasonEvent, SeasonDefinitions.SeasonInterval seasonInterval, CareerSeasonLayer seasonLayer)
	{
		UIWidget component = GetComponent<UIWidget>();
		this.seasonInterval = seasonInterval;
		this.seasonLayer = seasonLayer;
		isEventFinished = false;
		if (seasonInterval.isCurrentInterval)
		{
			TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = GGPlayerSettings.instance.careerStatsManager.GetActiveEvent(seasonEvent.namedEventId);
			if (activeEvent != null)
			{
				isEventFinished = activeEvent.isFinished;
			}
		}
		component.alpha = ((!isEventFinished) ? 1f : 0.5f);
		Debug.Log("isEventFinished " + isEventFinished);
		this.seasonEvent = seasonEvent;
		base.gameObject.SetActive(true);
		titleLabel.text = seasonEvent.eventName;
		Events.EventDefinition eventById = EventsDB.instance.GetEventById(seasonEvent.eventDefinitionId);
		Events.Tournament tournament = eventById as Events.Tournament;
		if (tournament != null)
		{
			subtitleLabel.text = "Tournament Rating:";
			SetStarsCount(tournament.starCount);
			GameConstants.SetFlag(headerSprite, (int)seasonEvent.country);
		}
	}

	public void SetStarsCount(int starsCount)
	{
		int num = 0;
		foreach (UISprite star in stars)
		{
			Color color = ((num > starsCount) ? starOffColor : Color.white);
			star.color = color;
			num++;
		}
	}

	public void OnClick()
	{
		if (!seasonInterval.isCurrentInterval)
		{
			if (!seasonInterval.isAfterCurrentInterval)
			{
				return;
			}
			TableTennisDialog.instance.ShowYesNo(string.Empty, "You still have some events in the current month. Do you want to skip over to " + seasonInterval.intervalName, "Yes", "No", delegate(bool success)
			{
				if (success)
				{
					TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
					SeasonDefinitions.SeasonId seasonId = (SeasonDefinitions.SeasonId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId;
					SeasonDefinitions.Season season = SeasonDefinitions.instance.GetSeason(seasonId);
					careerStatsManager.BeginSeasonInterval(season, seasonInterval);
					seasonLayer.Init();
				}
				NavigationManager.instance.Pop();
			});
		}
		else if (isEventFinished)
		{
			TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = GGPlayerSettings.instance.careerStatsManager.GetActiveEvent(seasonEvent.namedEventId);
			Events.EventDefinition @event = seasonEvent.GetEvent();
			Events.EventStage stage = @event.GetStage(activeEvent.stageIndex);
			string text = " you reached " + stage.stageName;
			if (activeEvent.stageIndex >= @event.tournamentStages.Count)
			{
				text = " you won ";
			}
			TableTennisDialog.instance.ShowOk(string.Empty, seasonEvent.eventName + " has finished," + text + "! You can play it again in the next season.", "Ok", null);
		}
		else
		{
			tournamentLayer.ShowWithEvent(seasonEvent);
		}
	}
}
